package interface_layer;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import processing_layer.Demo;
import processing_layer.SessionTracker;

import database_layer.Common;
import database_layer.DatabaseManager;

/**
 * This servlet monitors the tomcat server.
 * We need to know when the server is up, and when it's down, in order to
 * be able to initialize / destroy the DB, and update our session handling
 * modules.
 */
public final class LifecycleMonitor implements ServletContextListener
{
	private ServletContext context = null;

	public LifecycleMonitor() {}

	//This method is invoked when the Web Application
	//has been removed and is no longer able to accept
	//requests

	public void contextDestroyed(ServletContextEvent event)
	{
		//Output a simple message to the server's console
	    System.out.println("Apache's down!!!");
		try
		{
			DatabaseManager.destroyDB();
		}
		catch(Exception e)
		{
			System.out.println(e.getLocalizedMessage());
		}
		
		SessionTracker.cleanEntries();
		System.out.println("Cleaned all tracked sessions...");
	    this.context = null;

	}

	//This method is invoked when the Web Application
	//is ready to service requests

	public void contextInitialized(ServletContextEvent event)
	{
		this.context = event.getServletContext();
		  
		//Output a simple message to the server's console
		System.out.println("Apache's UP... Configuring the DB!!!");
		try
		{
			String driver = context.getInitParameter(Common.initDriverName);
			String connectionLink = context.getInitParameter(Common.initDatabaseAddress);
			String username = context.getInitParameter(Common.initDatabaseUsername);
			String password = context.getInitParameter(Common.initDatabasePasword);
			String testMode = context.getInitParameter(Common.initTestModeIndicator);
			boolean inTestMode = false;
			
			/* Testing that all the variables are present */
			if (null == driver ||
				null == connectionLink ||
				null == username ||
				null == password)
			{
				System.out.println("***** ERROR ERROR ERROR *****");
				System.out.println("***** NOT ALL PARAMETERS ARE PRESENT IN WEB.XML, DB WON'T BE INITIALIZED! *****");
				System.out.println("***** ERROR ERROR ERROR *****");				
			}
			else
			{    
				if (null != testMode && "true".equals(testMode))
	            {	
					inTestMode = true;
	            }
				
		        System.out.println("Sending to DB: " + 
		                           driver + ", " +
		                           connectionLink + ", " + 
		                           username + ", " + 
		                           password + ", " + 
		                           inTestMode);
		        DatabaseManager.initializeValues(driver, connectionLink, username, password, inTestMode);
		        DatabaseManager.initDB();
		        Demo.insertDemoValues();
			}
		}
		catch(Exception e)
		{
			System.out.println("***** ERROR ERROR ERROR *****");
			System.out.println("***** DB NOT INITIALIZED PROPERLY *****");
			System.out.println("***** ERROR ERROR ERROR *****");
		}
		SessionTracker.cleanEntries();
		System.out.println("Cleaned all tracked sessions...");
	  }

}